﻿#region 版权信息
// ------------------------------------------------------------------------------
// Copyright: (c) 2029  耍嗨科技（成都）有限公司 - Amm
// 项目名称：$SOLUTION/$.Amm.Common
// 文件名称：MethodInfoExtensions.cs
// 创建时间：2020093011:17
// 更改时间：2020093011:17
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Colorful.Furniture.Extensions
{
    public static class MethodInfoExtensions
    {
        public static bool HasAttribute<T>(this MethodInfo method)
        {
            return method.GetCustomAttributes(typeof(T), false).FirstOrDefault() is T;
        }

        public static T GetAttribute<T>(this MethodInfo method) where T : Attribute
        {
            return method.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
        }

        public static bool IsAsync(this MethodInfo method)
        {
            return method.ReturnType == typeof(Task)
                   || (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>));

        }
    }
}